<?php
namespace Admin\Controller;
use Admin\Api\CarApi;
use Admin\Api\PictureApi;
use Think\Controller;
use Admin\Api\CarTermApi;
use Think\Upload;
class CarManageController extends BaseController {

    /*
     * 增加车辆
     * */
    public function add(){
        if(IS_POST){
            $res = $this->uploadPicture($_FILES);
            if($res){
                $imgs = '';
                foreach($res as $vo){
                    $picture = array(
                        'path'=> '/Uploads/Car/'.$vo['savepath'].$vo['savename'],
                        'uid'=> '1',
                        'ori_name'=>$vo['name'],
                        'savename'=>$vo['savename'],
                        'imgurl' => '/Uploads/Car/'.$vo['savepath'].$vo['savename'],
                        'md5' => $vo['md5'],
                        'sha1' => $vo['sha1'],
                        'status'=>'1',
                        'create_time'=>time(),
                        'type'=> $vo['ext'],
                    );
                    $pictureres = apiCall(PictureApi::ADD,array($picture));
                    if($pictureres['status']){
                        $imgs = $imgs.','.$pictureres['info'];
                    }
                }
                $add = array(
                    'name'=>I('name'),
                    'subordinate_term	'=>I('subordinate_term	'),
                    'plate_number'=>I('plate_number'),
                    'serial_number'=>I('serial_number'),
                    'color'=>I('color'),
                    'picture_id'=>trim($imgs,','),
                    'price1'=>I('price1'),
                    'price2'=>I('price2'),
                    'price3'=>I('price3'),
                    'price4'=>I('price4'),
                    'price5'=>I('price5'),
                    'introduce'=>I('introduce'),
                    'car_case'=>I('car_case'),
                    'car_address'=>I('car_address'),
                    'sell_price'=>I('sell_price'),
                    'create_time'=>time(),
                    'update_time'=>time()
                );

                $carres = apiCall(CarApi::ADD,array($add));

                if($carres['status']){
                   $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error('图片上传失败');
            }

        }else{
            $this->display();
        }
    }

    /*
     * 编辑
     * */
    public function edit(){
        if(IS_POST){

        }else{

            $this->display();
        }
    }

    /*
     * 先增加一个文件上传类
     * 到时候独立出去
     * */
    public function uploadPicture($files){
        $config = array(
            'rootPath' => './Uploads/Car/',
        );
        $Upload = new Upload($config);
        $info   = $Upload->upload($files);

        return $info;
    }

}